<!--单点登录-->
<template>
	<view class="main">
		<ul>
			<h3 style="margin-bottom: 20px; display: flex; align-items: center;">
				<van-icon name="arrow-left" @click="exit" />
				<span style="flex: 1; text-align: center;">应用中心</span>
			</h3>
			<li v-for="(item,index) in menuList" :key="index">
				<view @click="jump(item.menuName)">
					<img :src=item.imgUrl alt="">
					<text :class="item.disabled ? 'title-menu-disabled' : 'title-menu'">{{ item.menuName }}</text>
				</view>
			</li>
		</ul>
	</view>
</template>

<script>
	import baseConfig from "../common/config";
	import {
		mapMutations,
		mapGetters
	} from "vuex"
	import {
		Dialog,
		Toast
	} from "vant";
	let config = {};
	let menuList = [
      {
			imgUrl: require("../static/longin/loginxj.png"), // 图标
			menuName: "现场巡检", // 应用名称
			systemType: "APPXTGL", // 应用id，取值为fixed时表示固定显示
			disabled: false // 是否禁用
		},
		{
			imgUrl: require("../static/longin/loginxj.png"), // 图标
			menuName: "常规检查", // 应用名称
			systemType: "CGJC", // 应用id，取值为fixed时表示固定显示
			disabled: false // 是否禁用
		},
		{
			imgUrl: require("../static/longin/loginxj.png"), // 图标
			menuName: "会议电源巡视", // 应用名称
			systemType: "HYDYXS", // 应用id，取值为fixed时表示固定显示
			disabled: false // 是否禁用
		},
		{
			imgUrl: require("../static/longin/loginyw.png"),
			menuName: "工单审批",
			systemType: "APPGDSP",
			disabled: false
		},
		{
			imgUrl: require("../static/longin/logingcgk.png"),
			menuName: "工程管控",
			systemType: "APPGCGK",
			disabled: false
		},
		{
			imgUrl: require("../static/longin/loginbzhzy.png"),
			menuName: "标准化作业",
			systemType: "APPBZZY",
			disabled: false
		},
		{
			imgUrl: require("../static/img/01.png"),
			menuName: "AI助手",
			systemType: "fixed",
			disabled: true
		},
			{
			imgUrl: require("../static/longin/loginbzhzy.png"),
			menuName: "现场检修作业",
			systemType: "XCJXZY",
			disabled: false
		},
		{
			imgUrl: require("../static/img/dbrw.png"),
			menuName: "作业知识助手",
			systemType: "ZYZSZS",
			disabled: false
		},
		{
			imgUrl: require("../static/img/wgscrdjb_sh.png"),
			menuName: "个人中心管理",
			systemType: "GRZXGL",
			disabled: false
		},
		{
			imgUrl: require("../static/img/ydtz_icon.png"),
			menuName: "移动台账",
			systemType: "APPLEDGER",
			disabled: false
		},
	]
	export default {
		data() {
			return {
				wx_code: '',
				ticket: '',
				accessToken: '',
				account: '', // uat环境用户名 wb_qinhuixin  //sx_zhangxia
				appid: '',
				getCodeUrl: '',
				getTicketUrl: '',
				isUAT: true, //true uat 环境 false 正式环境
				menuList: [],
			}
		},
		onLoad() {
			// 勿删
			Toast.loading({
				duration: 0,
				message: '程序正在加载中...',
				forbidClick: true,
			});
            // this.getApps();  // pc开发放开 打包注释
			// #ifndef IGW
			// this.getApps();
			// #endif

			baseConfig.getOptions.then(res => {
				config = res;

				// #ifdef  IGW-UAT
				this.getCodeUrl = 'https://igw.isgcc.net:18081/connect/oauth2/authorize?';
				this.appid = 'ww445f8033443a14aa';
				this.getTicketUrl = 'https://igw.isgcc.net:18443/proxy/getUserByIgwCode';
				this.agentid = '1007818'
				// #endif

				// #ifdef  IGW-PRODUCTION
				this.getCodeUrl = 'https://igw.sgcc.com.cn/connect/oauth2/authorize?';
				this.appid = 'ww4d11a39991ebffdc';
				this.getTicketUrl = 'https://id.sgcc.com.cn:10443/igwmobile/proxy/getUserByIgwCode';
				this.agentid = '1002033'
				// #endif

				// #ifdef IGW
				this.getwxcode();
				// #endif
			})
		},
		computed: {
			...mapGetters(["roleAuth"]),
		},
		methods: {
			...mapMutations(["userDataSet", "setAccount"]),
			/**-------------单点登录开始----------------*/
			getwxcode(istest) {
				this.isTest = istest;
				if (this.getParam('code')) {
					this.wx_code = this.getParam('code');
					this.getticket();
				} else {
					var SURL = this.getCodeUrl;
					SURL += 'appid=' + this.appid;
					SURL += '&redirect_uri=' + encodeURIComponent('zipapp://local.host/index.html');
					SURL += '&response_type=code';
					SURL += '&scope=snsapi_base';
					SURL += '&state=STATE';
					SURL += '&agentid=' + this.agentid;
					SURL += '#wechat_redirect';
					location.replace(SURL)
				}
			},
			// 获取URL参数
			getParam(variable) {
				var query = window.location.search.substring(1);
				var vars = query.split("&");
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split("=");
					if (pair[0] == variable) {
						return pair[1];
					}
				}
				return '';
			},

			getticket() {
				uni.request({
					url: this.getTicketUrl,
					method: 'POST',
					header: {
						'Content-Type': 'application/json;charset=utf-8',
					},
					data: {
						CODE: this.wx_code,
						appId: this.agentid
					}
				}).then(res => {
					console.log(res[1].data.code, 'res[1].data.code');
					if (res[1].data.code === '100001') {
				    this.ticket = res[1].data.data
            console.log(this.ticket,'this.ticket');
            this.getDecrypt()
					}
				})
			},
      getDecrypt(res) {
        let params = {
           ticket: this.ticket,
           type:'UAT'
        }
        // #ifdef  IGW-PRODUCTION
         params = {
          ticket: this.ticket,
          type:'PRODUCTION'
        }
        // #endif
        this.$api.getDecrypt(params).then(res => {
          console.log(res,'解密后')
          this.account = res.data.nameCode
          this.getApps()
        })
      },

			/**-------------单点登录结束----------------*/

			// 获取应用列表
			getApps() {
				let params = {
					"account": this.account
				};
				this.$api.getApps(params).then(res => {
					Toast.clear();
					if (res.code == 0) {
						uni.setStorageSync('token', res.data?.access_token)
						this.account = res.data.account;
						let appdata = res.data.menuList.map(ite => {
							return ite.systemType
						})
						this.menuList = menuList.filter(item => {
							// return appdata?.includes(item.systemType) || item.systemType === 'fixed'
							return appdata?.includes(item.systemType)
						});
					} else {
						Dialog.confirm({
							message: "载入应用失败",
							confirmButtonText: "重新加载",
							cancelButtonText: "返回工作台"
						}).then(() => {
							Toast.loading({
								duration: 3000,
								message: '加载中，请稍候',
								forbidClick: true,
							});
							setTimeout(() => {
								this.getApps();
							}, 3000)
						}).catch(() => {
							this.exit();
						});
					}
				}).catch(error => {
					Toast.clear();
					Dialog.confirm({
						message: "载入应用失败",
						confirmButtonText: "重新加载",
						cancelButtonText: "返回工作台"
					}).then(() => {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							this.getApps();
						}, 3000)
					}).catch(() => {
						this.exit();
					});
				});
			},
	    // 常规检查
			autumnInspectionLogin(){
				this.$api.getLogin({
					"account": this.account
				}).then((res) => {
					uni.reLaunch({
							url: `/pages/autumnInspection/index`
						});
					if (res.code === 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: `/pages/autumnInspection/index`
						});
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}
				});
			},
			// 现场巡检登录
			inspectionLogin() {
				this.$api.getLogin({
					"account": this.account
				}).then((res) => {
					if (res.code === 0) {
						this.userDataSet(res.data);
						if (res.data.roleId === "5b6c83a5-555d-4bbc-83ff-3de222466947") {
							uni.reLaunch({
								url: `/pages/testProject/index`
							});
						} else if (this.roleAuth !== "NOAUTH") {
							uni.reLaunch({
								url: "/pages/index/index"
							});
						} else {
							Toast.loading({
								duration: 3000,
								message: '加载中，请稍候',
								forbidClick: true,
							});
							setTimeout(() => {
								Dialog.alert({
									message: "加载失败",
									confirmButtonText: "返回工作台",
									theme: "round-button",
								}).then(() => {
									this.exit();
								});
							}, 3000)
						}
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}
				});
			},
			// 运维审批登录
			operationLogin() {
				this.setAccount(this.account)
				this.$api.getPermissions({
					account: this.account
				}).then((res) => {
					if (res.code === 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: "/pages/operationApprove/index/index"
						});
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}
				})
			},
			// 工程管控
			projectControl(item) {
				this.$apiRequest.gcgkApi.getLogingcgk({
					"account": this.account
					// "account": item.account
				}).then((res) => {
					if (res.code == 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: "/pages/communicationControl/index/index"
						});
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}
					console.log(res, 323232)
				})
			},
			// 标准化作业
			standardJob() {
				this.$apiRequest.bzhzyApi.selectuserinfo({
					account: this.account
				}).then(res => {
					if (res.code === 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: "/pages/standardizedOperation/index"
						})
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}

				}).catch(error => {
					console.log("---", error)
				})

			},
			// 现场检修作业 -  验收版
			standardJob2() {
				// uni.reLaunch({
				// 	url: "/pages/acceptanceVersion/index"
				// })
				this.setAccount(this.account)
				this.$apiRequest.bzhzyApi.selectuserinfo({
					account: this.account
				}).then(res => {
					if (res.code === 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: "/pages/acceptanceVersion/index"
						})
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}

				}).catch(error => {
					console.log("---", error)
				})
			},
			// 会议电源巡视 -  离线版
			inspectionMeeting(){
				uni.reLaunch({
					url: "/pages/inspectionMeeting/index"
				})
			},
			standardJob3() {
				// uni.reLaunch({
				// 	url: "/pages/acceptanceVersion/knowledgeAssistant"
				// })
				this.setAccount(this.account)
				this.$apiRequest.bzhzyApi.selectuserinfo({
					account: this.account
				}).then(res => {
					if (res.code === 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: "/pages/acceptanceVersion/knowledgeAssistant"
						})
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}

				}).catch(error => {
					console.log("---", error)
				})
			},
			standardJob4() {
				// uni.reLaunch({
				// 	url: "/pages/acceptanceVersion/mine/mine"
				// })
				this.setAccount(this.account)
				this.$apiRequest.bzhzyApi.selectuserinfo({
					account: this.account
				}).then(res => {
					if (res.code === 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: "/pages/acceptanceVersion/mine/mine"
						})
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}

				}).catch(error => {
					console.log("---", error)
				})
			},
			// 移动台账
			mobileLedger() {
				// uni.reLaunch({
				// 	url: "/pages/acceptanceVersion/mine/mine"
				// })
				this.setAccount(this.account)
				this.$apiRequest.ydtzApi.getMenus({
					account: this.account
				}).then(res => {
					if (res.code === 0) {
						this.userDataSet(res.data);
						uni.reLaunch({
							url: "/pages/standingBook/index"
						})
					} else {
						Toast.loading({
							duration: 3000,
							message: '加载中，请稍候',
							forbidClick: true,
						});
						setTimeout(() => {
							Dialog.alert({
								message: "加载失败",
								confirmButtonText: "返回工作台",
								theme: "round-button",
							}).then(() => {
								this.exit();
							});
						}, 3000)
					}
				}).catch(error => {
					console.log("---", error)
				})
			},
			jump(title) {
				if (title === '现场巡检') {
					this.inspectionLogin();
				}  else if (title === '常规检查') {
					this.autumnInspectionLogin()
				} else if (title === '会议电源巡视') {
					this.inspectionMeeting()
				} else if (title === '工单审批') {
					this.operationLogin()
				} else if (title === '工程管控') {
					this.projectControl()
				} else if (title === '标准化作业') {
					this.standardJob()
				} else if (title === '现场检修作业') {
					this.standardJob2()
				} else if (title === '作业知识助手') {
					this.standardJob3()
				} else if (title === '个人中心管理') {
					this.standardJob4()
				}else if (title === '移动台账') {
					this.mobileLedger()
				}
			},

			exit() {
				baseConfig.actions.exitIgw();
			}
		},
	}
</script>

<style lang="scss" scoped>
	.main {
		height: 100vh;
		width: 100vw;
		position: relative;
		background: #f2f6fa;

		ul {
			width: px2vw(1000);
			margin-left: px2vw(40);
			padding-top: px2vh(40);
			height: calc(100vh - #{px2vh(80)});

			li {
				width: calc(1 * (100% - #{px2vw(80)}) / 4);
				float: left;
				margin-left: px2vw(20);
				height: px2vh(240);
				text-align: center;

				img {
					display: block;
					width: px2vw(146);
					height: px2vw(146);
					margin-left: px2vw(40);
					margin-bottom: px2vh(20);
				}

				.title-menu {
					font-size: px2vw(36);
					color: #333;
				}

				.title-menu-disabled {
					font-size: px2vw(36);
					color: #ccc;
				}
			}

			//li:nth-child(2) {
			//   .title-menu {
			//     color: #333;
			//   }
			// }
		}
	}
</style>
